package com.hyyt.shopjsp.phoneApp.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.phoneApp.pojo.PhoneAppHomeCategory;
import com.hyyt.shopjsp.phoneApp.service.IPhoneAppHomeCategoryService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.tree.NodeUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * phone_app_home_category,PHONE_手机APP_端广告分类 Controller
 *
 */

@Controller
@RequestMapping("/back/phoneAppHomeCategory")
public class PhoneAppHomeCategoryController extends BaseController {

    private Logger log = Logger.getLogger(PhoneAppHomeCategoryController.class);
    private String basePath = "/back/phoneApp/phoneAppCategory/";

    @Resource
    private IPhoneAppHomeCategoryService phoneAppHomeCategoryService;

    /**
     * 跳转到手机APP端广告分类
     *
     * @return 进入指定页面
     */
    @RequestMapping("gotoPhoneAppHomeCategoryPage")
    public String gotoPhoneAppHomeCategoryPage(Map<String, Object> param){
        param.put("categoryId", "0");//手机首页分类id
        return basePath+"phoneAppHomeCategoryList";
    }

    /**
     * 查询所有手机APP端广告分类
     *
     * @param page
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "listPhoneAppHomeCategory", method = RequestMethod.POST)
    @ResponseBody
    public void listPhoneAppHomeCategory(Page page,String categoryId,String categoryName,HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        param.put("categoryId", categoryId);
        param.put("categoryName", categoryName);
        int totalRecordCount = phoneAppHomeCategoryService.countPhoneAppHomeCategory(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<PhoneAppHomeCategory> list = phoneAppHomeCategoryService.queryPhoneAppHomeCategory(param, page.getPageRecordBeginIndex(), page.getPageSize());
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 根据手机APP端广告分类id获得分类对象
     * @param categoryId
     * @return
     */
    @RequestMapping(value="getPhoneAppHomeCategoryObj",method= RequestMethod.POST)
    @ResponseBody
    public ResultMsg getPhoneAppHomeCategoryObj(Integer categoryId){
        if(Utils.objectIsNotEmpty(categoryId)){
            PhoneAppHomeCategory phoneAppHomeCategory = phoneAppHomeCategoryService.loadPhoneAppHomeCategory(categoryId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(phoneAppHomeCategory));
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或修改手机APP端广告分类
     * @param phoneAppHomeCategory
     * @return
     */
    @RequestMapping(value="saveOrEditPhoneAppHomeCategory",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrEditPhoneAppHomeCategory(PhoneAppHomeCategory phoneAppHomeCategory){
        if(Utils.objectIsEmpty(phoneAppHomeCategory.getCategoryId())){//判断为空
            phoneAppHomeCategoryService.savePhoneAppHomeCategory(phoneAppHomeCategory);//添加操作
        }else{
            phoneAppHomeCategoryService.updatePhoneAppHomeCategory(phoneAppHomeCategory);
        }
        return ResultMsg.create(true);
    }

    /**
     * 删除手机APP端广告分类
     * @param categoryId
     * @return
     */
    @RequestMapping(value="delPhoneAppHomeCategory",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg delPhoneAppHomeCategory(Integer categoryId){
        if(Utils.objectIsNotEmpty(categoryId)){
            PhoneAppHomeCategory categoryTemp =phoneAppHomeCategoryService.loadPhoneAppHomeCategory(categoryId);//拿到原对象
            phoneAppHomeCategoryService.deletePhoneAppHomeCategory(categoryId);
            Integer parentIdTemp = categoryTemp.getParentId();
            Map<String, Object> param=new HashMap<>();
            param.put("parentId", parentIdTemp);
            List<PhoneAppHomeCategory> ba =phoneAppHomeCategoryService.queryPhoneAppHomeCategory(param);
            if(null == ba || ba.size() ==0){//将父类修改为叶子节点
                PhoneAppHomeCategory bap=phoneAppHomeCategoryService.loadPhoneAppHomeCategory(parentIdTemp);
                bap.setIsLeaf(1);
                phoneAppHomeCategoryService.updatePhoneAppHomeCategory(bap);
            }
        }
        return ResultMsg.create(true);
    }

    /**
     * 根据id展示左侧新闻树形结构
     * @param response
     */
    @RequestMapping("getNodes")
    public void getNodes(HttpServletResponse response){
        List<PhoneAppHomeCategory> phoneAppHomeCategoryList =  phoneAppHomeCategoryService.queryAllPhoneAppHomeCategory();
        List nodeList = new ArrayList();
        HashMap nodeMap = new HashMap();
        for(int i = 0; i < phoneAppHomeCategoryList.size(); i++) {
            PhoneAppHomeCategory phc = phoneAppHomeCategoryList.get(i);
            nodeMap = new HashMap();
            nodeMap.put("id",phc.getCategoryId());
            nodeMap.put("text", phc.getCategoryName());
            nodeMap.put("isLeaf", phc.getIsLeaf());
            String parentId = String.valueOf(phc.getParentId());
            if (Utils.objectIsEmpty(parentId)) {
                parentId = "0";
            }
            nodeMap.put("parentId",parentId);
            nodeList.add(nodeMap);
        }
        //获取分类的json字符串信息
        String jsonInfo= NodeUtil.getNodeJson(nodeList,"手机APP端广告分类");
        ResultMsg.create(jsonInfo, response, false);
    }

}
